﻿#include "VTKPCL.h"
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkAxesActor.h>
#include <vtkOBJReader.h>
#include <pcl/io/pcd_io.h>            
#include <pcl/io/ply_io.h>
#include <pcl/io/vtk_io.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/kdtree/kdtree_flann.h>  
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d.h>
#include <pcl/common/transforms.h>
#include <pcl/surface/gp3.h>
#include <pcl/surface/poisson.h>
#include <pcl/surface/mls.h>          
#include <pcl/surface/marching_cubes_hoppe.h>
#include <pcl/surface/marching_cubes_rbf.h>

VTKPCL::VTKPCL(QWidget* parent)
	: QMainWindow(parent)
{
	vtkWidget = new QVTKOpenGLNativeWidget(this);
	this->resize(600, 400);
	this->setCentralWidget(vtkWidget);

	cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);


	pcl::io::loadPCDFile("D:/TEST/3D/pcb/rabbit.pcd", *cloud);

	// 整体移动
	pcl::transformPointCloud(*cloud, *cloud,
		Eigen::Affine3f(Eigen::Translation3f(0.1, 0, 0.1)));


	auto renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
	auto renderer = vtkSmartPointer<vtkRenderer>::New();
	renderWindow->AddRenderer(renderer);

	viewer = pcl::visualization::PCLVisualizer::Ptr(new pcl::visualization::PCLVisualizer(
		renderer
		, renderWindow, "", false));
	viewer->addPointCloud(cloud, "cloud");
	viewer->updatePointCloud(cloud, "cloud");
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");

	vtkWidget->setRenderWindow(viewer->getRenderWindow());
	viewer->setupInteractor(vtkWidget->interactor(), vtkWidget->renderWindow());


	vtkNew<vtkAxesActor> axes;
	axes->SetTotalLength(0.1, 0.1, 0.1);
	renderer->AddActor(axes);
}

VTKPCL::~VTKPCL()
{
	vtkWidget->interactor()->SetRenderWindow(nullptr);
	vtkWidget->interactor()->SetInteractorStyle(nullptr);
}
